package com.fh.controller.ccode.talents;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.fh.controller.base.BaseController;
import com.fh.service.ccode.AppCcodeUserService;
import com.fh.service.ccode.TalentsService;
import com.fh.util.AppUtil;
import com.fh.util.Const;
import com.fh.util.DateUtil;
import com.fh.util.FileUpload;
import com.fh.util.PageData;
import com.fh.util.PathUtil;
import com.fh.util.TimeUtil;

@Controller
@RequestMapping(value = "/apptalents")
public class TalentsController extends BaseController {
	@Resource(name = "talentsService")
	private TalentsService talentsService;

	@Resource(name = "appCcodeUserService")
	private AppCcodeUserService appCcodeUserService;

	@RequestMapping(value = "/talist")
	@ResponseBody
	public Object getTalentsList(HttpServletRequest request, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();

		int start = (Integer.parseInt(pd.getString("pageNum")) - 1) * Integer.parseInt(pd.getString("pageSize"));
		pd.put("start", start);

		int size = Integer.parseInt(pd.getString("pageSize"));
		pd.put("size", size);

		pd.remove("pageNum");
		pd.remove("pageSize");

		try {

			String role = appCcodeUserService.getInfoByToken(pd).getString("ROLE");
			if (role.equals("REVIEWER")) {

				List<PageData> list = talentsService.getTalentsList(pd);
				List<PageData> tempList = new ArrayList<PageData>();

				for (int i = 0; i < list.size(); i++) {
					PageData tempPD = new PageData();
					tempPD.put("name", list.get(i).getString("NAME"));
					tempPD.put("avatarUrl", list.get(i).getString("HEAD_URL"));
					tempPD.put("detail", list.get(i).getString("DESCRIPTION"));
					tempPD.put("className", list.get(i).getString("CLASS_NAME"));
					tempPD.put("location", list.get(i).getString("CITY_NAME"));
					tempPD.put("school", list.get(i).getString("DEPARTNAME"));
					tempPD.put("score", list.get(i).get("SCORE") + "");
					tempPD.put("id", list.get(i).get("C_USER_ID") + "");
					
					tempList.add(tempPD);
				}
				map.put("data", tempList);
				map.put("code", "T");
				map.put("msg", "获取成功");
			} else if (role.equals("ENTERPRISE")) {
				List<PageData> list = talentsService.getTalentsListForEnterprise(pd);
				List<PageData> tempList = new ArrayList<PageData>();

				for (int i = 0; i < list.size(); i++) {
					PageData tempPD = new PageData();
					tempPD.put("name", list.get(i).getString("NAME"));
					tempPD.put("avatarUrl", list.get(i).getString("HEAD_URL"));
					tempPD.put("detail", list.get(i).getString("DESCRIPTION"));
					tempPD.put("className", list.get(i).getString("CLASS_NAME"));
					tempPD.put("location", list.get(i).getString("CITY_NAME"));
					tempPD.put("school", list.get(i).getString("DEPARTNAME"));
					tempPD.put("score", list.get(i).get("SCORE") + "");
					tempPD.put("id", list.get(i).get("C_USER_ID") + "");
					
					tempList.add(tempPD);
				}
				map.put("data", tempList);
				map.put("code", "T");
				map.put("msg", "获取成功");
			} else {
				map.put("code", "F");
				map.put("msg", "没有权限访问人才库");
			}

		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "F");
			map.put("msg", "获取失败");
		}

		return AppUtil.returnObject(new PageData(), map);
	}

	@RequestMapping(value = "/getOnePerson")
	@ResponseBody
	public Object getOnePerson(HttpServletRequest request, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();

		try {
			PageData info = appCcodeUserService.getInfoByToken(pd);
			PageData ret = talentsService.getInfoByID(pd);
			String role = info.getString("ROLE");
			String token = ret.getString("TOKEN");
			
			if (role.equals("REVIEWER") || token.equals(pd.getString("token")) 
					|| (role.equals("ENTERPRISE") && talentsService.checkPermission(pd) > 0)) {
				PageData tempPD = new PageData();
				tempPD.put("location", ret.getString("CITY_NAME"));
				tempPD.put("school", ret.getString("DEPARTNAME"));
				tempPD.put("className", ret.getString("CLASS_NAME"));
				tempPD.put("name", ret.getString("NAME"));
				tempPD.put("avatarUrl", ret.getString("HEAD_URL"));
				tempPD.put("content", ret.getString("DESCRIPTION"));

				map.put("data", tempPD);
				map.put("code", "T");
				map.put("msg", "获取成功");

			} else {
				map.put("code", "F");
				map.put("msg", "没有权限访问人才库");
			}

		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "F");
			map.put("msg", "获取失败");
		}

		return AppUtil.returnObject(new PageData(), map);
	}

	@RequestMapping(value = "/getPerSoList")
	@ResponseBody
	public Object getOneSolutionList(HttpServletRequest request, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();

		try {

			PageData info = appCcodeUserService.getInfoByToken(pd);
			PageData person = talentsService.getInfoByID(pd);
			String role = info.getString("ROLE");
			String token = person.getString("TOKEN");
			
			if (role.equals("REVIEWER") || token.equals(pd.getString("token"))
					|| (role.equals("ENTERPRISE") && talentsService.checkPermission(pd) > 0)) {
				List<PageData> list = talentsService.getOneSolutionList(pd);
				List<PageData> tempList = new ArrayList<PageData>();
				for (int i = 0; i < list.size(); i++) {
					PageData tempPD = new PageData();
					tempPD.put("title", list.get(i).getString("S_TITLE"));
					tempPD.put("avatarUrl", list.get(i).getString("HEAD_URL"));
					tempPD.put("detail", list.get(i).getString("S_DESCRIPTION"));
					tempPD.put("className", list.get(i).getString("CLASS_NAME"));
					tempPD.put("writer", list.get(i).getString("NAME"));
					tempPD.put("money", list.get(i).get("APPLYMONEY") + "");
					tempPD.put("id", list.get(i).get("SOLUTION_ID") + "");

					tempList.add(tempPD);
				}
				map.put("data", tempList);
				map.put("code", "T");
				map.put("msg", "获取方案列表成功");

			} else {
				map.put("code", "F");
				map.put("msg", "没有权限访问人才系统");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "F");
			map.put("msg", "获取方案列表失败");
		}

		return AppUtil.returnObject(new PageData(), map);
	}

	@RequestMapping(value = "/getTaComments")
	@ResponseBody
	public Object getTaComments(HttpServletRequest request, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		try {
			PageData info = appCcodeUserService.getInfoByToken(pd);
			PageData person = talentsService.getInfoByID(pd);
			String role = info.getString("ROLE");
			String token = person.getString("TOKEN");
			
			if (role.equals("REVIEWER") || token.equals(pd.getString("token"))
					|| (role.equals("ENTERPRISE") && talentsService.checkPermission(pd) > 0)) {
				List<PageData> list = talentsService.getTaComments(pd);
				List<PageData> tempList = new ArrayList<PageData>();

				for (int i = 0; i < list.size(); i++) {
					PageData tempPD = new PageData();
					tempPD.put("avatarUrl", list.get(i).getString("HEAD_URL"));
					tempPD.put("name", list.get(i).getString("NAME"));
					tempPD.put("time", df.format(list.get(i).get("TA_COMMENT_POSTTIME")));
					tempPD.put("content", list.get(i).getString("TA_COMMENT_CONTENT"));

					tempList.add(tempPD);
				}
				map.put("data", tempList);
				map.put("code", "T");
				map.put("msg", "获取评论成功");

			} else {
				map.put("code", "F");
				map.put("msg", "m没权限访问，请联系管理员获取权限");
			}

		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "F");
			map.put("msg", "获取评论失败");
		}

		return AppUtil.returnObject(new PageData(), map);
	}

	@RequestMapping(value = "/addTaComment")
	@ResponseBody
	public Object addSoComment(HttpServletRequest request, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String, Object> map = new HashMap<String, Object>();

		PageData pd = new PageData();
		pd = this.getPageData();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		pd.put("posttime", df.format(new Date()));

		try {
			
			PageData info = appCcodeUserService.getInfoByToken(pd);
			PageData person = talentsService.getInfoByID(pd);
			String role = info.getString("ROLE");
			String token = person.getString("TOKEN");

			if (role.equals("REVIEWER") || token.equals(pd.getString("token"))
					|| (role.equals("ENTERPRISE") && talentsService.checkPermission(pd) > 0)) {

				PageData tempPD = appCcodeUserService.getInfoByToken(pd);
				pd.put("userID", tempPD.get("C_USER_ID"));

				talentsService.addTaComment(pd);
				map.put("code", "T");
				map.put("msg", "添加评论成功");
			} else {
				map.put("code", "F");
				map.put("msg", "没有权限访问");
			}

		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "F");
			map.put("msg", "添加评论失败");
		}

		return AppUtil.returnObject(new PageData(), map);
	}

	@RequestMapping(value = "/modifyInfo")
	@ResponseBody
	public Object modifyInfo(@RequestParam(value = "file") MultipartFile headImg, 
			@RequestParam(value="id") Integer id,
			@RequestParam(value="content") String content,
			@RequestParam(value="className") String className,
			@RequestParam(value="location")	String location,
			@RequestParam(value="school") String school,
			HttpServletRequest request,
			HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd.put("id", id);
		pd.put("content", content);
		pd.put("className", className);
		pd.put("location", location);
		pd.put("school", school);

//		String ffile = DateUtil.getDays();
		String fileName = "";
		try {
			if (null != headImg && !headImg.isEmpty()) {
				String filePath = PathUtil.getClasspath() + Const.HEADPATHIMG;
				fileName = FileUpload.fileUp(headImg, filePath, this.get32UUID());
				pd.put("headName", Const.HEADPATHIMG + fileName);
			} else {
				// 放入默认头像地址
				pd.put("headName", "resource/cute.jpg");
			}
			talentsService.modifyInfo(pd);
			map.put("code", "T");
			map.put("msg", "修改信息成功");

		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "F");
			map.put("msg", "修改信息失败");
		}

		return AppUtil.returnObject(new PageData(), map);
	}

	@RequestMapping(value = "/modifyImg")
	@ResponseBody
	public Object modifyImg(@RequestParam(value = "file") MultipartFile headImg, HttpServletRequest request,
			HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("id", 16);

		String ffile = DateUtil.getDays(), fileName = "";
		try {
			if (null != headImg && !headImg.isEmpty()) {
				String filePath = PathUtil.getClasspath() + Const.HEADPATHIMG + ffile;
				fileName = FileUpload.fileUp(headImg, filePath, this.get32UUID());
			} else {
				map.put("code", "F");
				map.put("msg", "头像不能为空");
			}

			pd.put("headName", fileName);

			talentsService.modifyImg(pd);
			map.put("code", "T");
			map.put("msg", "修改头像成功");

		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "F");
			map.put("msg", "修改头像失败");
		}

		return AppUtil.returnObject(new PageData(), map);
	}
	
	@RequestMapping(value="/addTime")
	@ResponseBody
	public Object addCodeTime(HttpServletResponse response)
	{
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String,Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		
		try
		{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			pd.put("posttime", sdf.format(new Date()));
			
			int id = (Integer) appCcodeUserService.getInfoByToken(pd).get("C_USER_ID");
			pd.put("id", id);
			
			talentsService.addCodeTime(pd);
			map.put("code", "T");
			map.put("msg", "添加时间成功");
			
		}catch(Exception e){
			e.printStackTrace();
			map.put("code", "F");
			map.put("msg", "添加时间失败");
		}
		
		return AppUtil.returnObject(new PageData(), map);
	}
	
	@RequestMapping(value="/topLan")
	@ResponseBody
	public Object getTopFiveLan(HttpServletResponse response)
	{
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String,Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		
		try
		{
			PageData info = appCcodeUserService.getInfoByToken(pd);
			PageData person = talentsService.getInfoByID(pd);
			String role = info.getString("ROLE");
			String token = person.getString("TOKEN");

			if (role.equals("REVIEWER") || token.equals(pd.getString("token"))
					|| (role.equals("ENTERPRISE") && talentsService.checkPermission(pd) > 0)) {
				List<PageData> list = talentsService.getTopFiveLand(pd);
				List<PageData> tempList = new ArrayList<PageData>();
				for(int i=0; i < list.size(); i++){
					PageData tempPD = new PageData();
					String lan = list.get(i).getString("CODE_LAN");
					int sec = Integer.parseInt( list.get(i).get("CODETIME") + "");
					if(lan != null && !lan.isEmpty())
					{
						tempPD.put("lan",lan.trim());
						tempPD.put("time", TimeUtil.codingTime(sec));
						tempList.add(tempPD);
					}
	//				else
	//				{
	//					tempPD.put("lan", "其他");
	//					tempPD.put("time", TimeUtil.codingTime(sec));
	//				}
				}
				
				map.put("data", tempList);
				map.put("code", "T");
				map.put("msg", "获取成功");
				
			} else {
				map.put("code", "F");
				map.put("msg", "没有权限访问");
			}
			
		}catch(Exception e){
			e.printStackTrace();
			map.put("code", "F");
			map.put("msg", "获取失败");
		}
		
		return AppUtil.returnObject(new PageData(), map);
	}
	
	@RequestMapping(value="/topIDE")
	@ResponseBody
	public Object getTopFiveIDE(HttpServletResponse response)
	{
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String,Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		
		try
		{
			PageData info = appCcodeUserService.getInfoByToken(pd);
			PageData person = talentsService.getInfoByID(pd);
			String role = info.getString("ROLE");
			String token = person.getString("TOKEN");

			if (role.equals("REVIEWER") || token.equals(pd.getString("token"))
					|| (role.equals("ENTERPRISE") && talentsService.checkPermission(pd) > 0)) {
				List<PageData> list = talentsService.getTopFiveIDE(pd);
				List<PageData> tempList = new ArrayList<PageData>();
				
				for(int i=0; i < list.size(); i++){
					PageData tempPD = new PageData();
					int sec = Integer.parseInt( list.get(i).get("CODETIME") + "");
					String ide = list.get(i).getString("CODE_IDE");
					if(ide!= null && !ide.isEmpty()){
						tempPD.put("ide", ide.trim());
						tempPD.put("time",TimeUtil.codingTime(sec));
						tempList.add(tempPD);
					}
	//				else{
	//					tempPD.put("ide", "其他");
	//					tempPD.put("time", TimeUtil.codingTime(sec));
	//				}
					
				}
				
				map.put("data", tempList);
				map.put("code", "T");
				map.put("msg", "获取成功");
			} else {
				map.put("code", "F");
				map.put("msg", "没有权限访问");
			}
			
		}catch(Exception e){
			e.printStackTrace();
			map.put("code", "F");
			map.put("msg", "获取失败");
		}
		
		return AppUtil.returnObject(new PageData(), map);
	}
	
	@RequestMapping(value="/lastFive")
	@ResponseBody
	public Object getLastFiveDaysTime(HttpServletResponse response)
	{
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String,Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		
		try
		{
			PageData info = appCcodeUserService.getInfoByToken(pd);
			PageData person = talentsService.getInfoByID(pd);
			String role = info.getString("ROLE");
			String token = person.getString("TOKEN");

			if (role.equals("REVIEWER") || token.equals(pd.getString("token"))
					|| (role.equals("ENTERPRISE") && talentsService.checkPermission(pd) > 0)) {
				List<PageData> list = talentsService.getLastFiveDaysTime(pd);
				List<PageData> tempList = new ArrayList<PageData>();
				for(int i = 0; i<list.size(); i++)
				{
					PageData temp = new PageData();
					temp.put("date", list.get(i).get("POSTTIME"));
					int sec = Integer.parseInt( list.get(i).get("CODETIME") + "");
					temp.put("time",TimeUtil.codingTime(sec));
					
					tempList.add(temp);
				}
				
				map.put("data", tempList); 
				map.put("code", "T");
				map.put("msg", "获取成功");
				
			} else {
				map.put("code", "F");
				map.put("msg", "没有权限访问");
			}
			
		}catch(Exception e){
			e.printStackTrace();
			map.put("code", "F");
			map.put("msg", "获取失败");
		}
		
		return AppUtil.returnObject(new PageData(), map);
	}
	
	@RequestMapping(value="/allTime")
	@ResponseBody
	public Object getAllDaysTime(HttpServletResponse response)
	{
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String,Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		
		try
		{
			PageData info = appCcodeUserService.getInfoByToken(pd);
			PageData person = talentsService.getInfoByID(pd);
			String role = info.getString("ROLE");
			String token = person.getString("TOKEN");

			if (role.equals("REVIEWER") || token.equals(pd.getString("token"))
					|| (role.equals("ENTERPRISE") && talentsService.checkPermission(pd) > 0)) {
				List<PageData> list = talentsService.getAllDaysTime(pd);
				List<PageData> tempList = new ArrayList<PageData>();
				for(int i = 0; i<list.size(); i++)
				{
					PageData temp = new PageData();
					temp.put("date", list.get(i).get("POSTTIME"));
					int sec = Integer.parseInt( list.get(i).get("CODETIME") + "");
					temp.put("time",TimeUtil.codingTime(sec));
					
					tempList.add(temp);
				}
				
				map.put("data", tempList); 
				map.put("code", "T");
				map.put("msg", "获取成功");
				
			} else {
				map.put("code", "F");
				map.put("msg", "没有权限访问");
			}
			
		}catch(Exception e){
			e.printStackTrace();
			map.put("code", "F");
			map.put("msg", "获取失败");
		}
		
		return AppUtil.returnObject(new PageData(), map);
	}

}
